package com.Tank.draw;

import java.util.Vector;

@SuppressWarnings({"all"})
public class EnemyTank extends Tank implements Runnable {//敌人坦克
    Vector<Shot> shots = new Vector<>();
    boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true) {
            //如果shots size()=0创建子弹放入shot集合
            if (isLive && shots.size() < 1) {
                Shot s = null;
                //判断方向
                switch (getDirect()) {
                    case 0://up
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1://right
                        s = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2://down
                        s = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3://lift
                        s = new Shot(getX(), getY() + 20, 3);
                        break;

                }
                shots.add(s);
                new Thread(s).start();
            }
            //根据坦克方向来继续移动
            switch (getDirect()) {
                case 0://上
                    //保持一个方向走多少步
                    for (int i = 0; i < 30; i++) {
                        if (getY() > 0) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1://右
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000) {
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3://下
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750) {
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 4://左
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0) {
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //随机改变坦克方向
            setDirect((int) (Math.random() * 4));
            //线程结束
            if (!isLive) {
                break;
            }
        }
    }
}
